#include <iostream>
#include <stdio.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

int compare(const void *a,const void *b)
{
       return strcmp(*(char **)a,*(char **)b);
}

int getSubnetMask(char ip[], int len)
{
    struct sockaddr_in *sin = NULL;
    struct ifaddrs *ifa = NULL, *ifList;
    string name[10];
    if (getifaddrs(&ifList) < 0)
    {
        return -1;
    }
    int i = 0;
    //找到所有网卡，并打印网卡相关信息
    for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
    {
        if(ifa->ifa_addr->sa_family == AF_INET)
        {
            string ifname(ifa->ifa_name);
            name[i]=ifname;
            i++;
            printf("interfaceName: %s\n", ifa->ifa_name);

            sin = (struct sockaddr_in *)ifa->ifa_addr;
            printf("ipAddress: %s\n", inet_ntoa(sin->sin_addr));

            sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
            printf("broadcast: %s\n", inet_ntoa(sin->sin_addr));

            sin = (struct sockaddr_in *)ifa->ifa_netmask;
            printf("subnetMask: %s\n\n", inet_ntoa(sin->sin_addr));
        }
    }
    //打印所有非空的网卡名
    for (int i=0; i<sizeof(name)/sizeof(name[0]); i++)
    {
        if(strcmp(name[i].c_str(),""))
        {
            printf("name%d=%s,",i,name[i].c_str());
        }
    }
    printf("\n");
    //从小到大排序
    qsort(name,10,sizeof(name[0]),compare);
    //查找最小网卡对应的ip
    for (int i=0; i<sizeof(name)/sizeof(name[0]); i++)
    {
        if (strcmp(name[i].c_str(),""))
        {
            for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
            {
                if(ifa->ifa_addr->sa_family == AF_INET)
                {
                    string ifname(ifa->ifa_name);
                    if (strcmp(name[i].c_str(),ifname.c_str()) == 0)
                    {
                        printf("check name=%s\n",ifname.c_str());
                        sin = (struct sockaddr_in *)ifa->ifa_addr;
                        char *ipAddress = inet_ntoa(sin->sin_addr);
                        //过滤掉local
                        if (strcmp(ipAddress,"127.0.0.1"))
                        {
                            strncpy(ip,ipAddress,len);
                            printf("last use name=%s,ip=%s",name[i].c_str(),ip);
                            freeifaddrs(ifList);
                            return 0;
                        }
                        else
                        {
                            printf("is local, not use");
                        }
                    }
                }
            }
        }
    }

    freeifaddrs(ifList);
    return -1;
}


int main(int argc, char *argv[])
{
    char ip[64];
    if(int ret = getSubnetMask(ip,sizeof(ip)) < 0)
    {
        printf("find ip error\n");
        return -1;
    }
    printf("ip=%s\n",ip);
    return 0;
}